﻿namespace MetaVideoEditorEvolved
{
	using System.Diagnostics;
	using System.Windows;
	using System.Windows.Input;
	using GalaSoft.MvvmLight.Messaging;
	using MetaVideoEditorEvolved.Messages;
	using MetaVideoEditorEvolved.ViewModel;

	/// <summary>
	/// Logique d'interaction pour Search.xaml
	/// </summary>
	public partial class Search : Window
	{
		public Search()
		{
			InitializeComponent();

            this.Owner = System.Windows.Application.Current.MainWindow;
            
            Messenger.Default.Register<CloseMessage>(this, (message) =>
			                                         {
			                                         	if (message.WindowToClose == WindowType.Search)
			                                         	{
			                                         		this.Close();
			                                         	}
			                                         });
		}

		private void NavigateHyperlink_Click(object sender, System.Windows.Navigation.RequestNavigateEventArgs e)
		{
			Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri));
			e.Handled = true;
		}

		private void ListBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
		{
			if(this.ListBoxMediaItem.SelectedItem!=null)
			{
				(DataContext as SearchVM).UpdateItemSelected();

				this.Close();
			}
		}
		
		private void TextBox_KeyDown(object sender, KeyEventArgs e)
		{
			if(e.Key == Key.Enter)
			{
				(DataContext as SearchVM).SearchMedia();
			}
		}
		
		private void SearchWindow_Loaded(object sender, RoutedEventArgs e)
		{
			SearchBox.Focus();
			
			SearchBox.SelectAll();
		}
	}
}
